message.js ➔ AddMessage   A
last analyzed

Complexity

Conditions 5

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 9.3333
c 0
b 0
f 0
cc 5
1
// var HomeMessage = function (name, success, error) {
2
//     this.name = name;
3
//     this.success = success;
4
//     this.error = error;
5
//
6
//     this.create = function (data) {
7
//         restSubmit(this.name, 'GET', data, this.success, this.error);
8
//     }
9
//
10
// };
11
12
13
var Message = function (selector) {
14
    this.selector = selector;
15
16
17
    this.add = function (message) {
18
        console.log(message);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
19
        var classname = null;
20
21
        if (typeof classname !== 'string') {
22
            classname = 'home-messages';
23
        }
24
        handle = document.getElementsByClassName(classname)
0 ignored issues
show
Bug introduced by
The variable handle seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.handle.
Loading history...
25
        if (handle)
26
27
            var node = document.createElement("LI");                 // Create a <li> node
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
28
        var textnode = document.createTextNode(message);         // Create a text node
29
        node.appendChild(textnode);
0 ignored issues
show
Bug introduced by
The variable node does not seem to be initialized in case handle on line 25 is false. Are you sure this can never be the case?
Loading history...
30
        document.getElementsByClassName(classname)[0].appendChild(node);
31
    }
32
}
33
34
35
function AddMessage(text, classname) {
36
    var message = new Message(classname);
37
    message.add(text);
38
}
39